package com.skillscompetition.skillscompetition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.skillscompetition.skillscompetition.bean.GroupResults;
import com.skillscompetition.skillscompetition.bean.Ranking;
import com.skillscompetition.skillscompetition.bean.SkillScontest;
import com.skillscompetition.skillscompetition.mapper.GroupResultsMapper;
import com.skillscompetition.skillscompetition.mapper.SkillScontestMapper;
import com.skillscompetition.skillscompetition.service.IGroupResultsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lwg
 * @since 2019-11-09
 */
@Service
public class GroupResultsServiceImpl extends ServiceImpl<GroupResultsMapper, GroupResults> implements IGroupResultsService {
    @Autowired
    GroupResultsMapper groupResultsMapper;

    @Autowired
    SkillScontestMapper skillScontestMapper;
    @Autowired
    SkillScontestServiceImpl skillScontestService;

    /**
     * 计算团体排名
     */
    @Override
    public void sort() {
        QueryWrapper<SkillScontest> wrapper = new QueryWrapper<>();
        List<SkillScontest> skillScontests = skillScontestMapper.getSkillSOperation(wrapper);
        for (SkillScontest skillScontest : skillScontests) {
            float total = (skillScontest.getSubtotal1() * 0.4f) + (skillScontest.getSubtotal2() * 0.3f) + (skillScontest.getSubtotal3() * 0.3f);
            skillScontest.setPracticalOperationSum(total);
            float personageScores = (skillScontest.getTheorys() * 0.4f) + (total * 0.6f);
            skillScontest.setPersonageScores(personageScores);
        }
        skillScontestService.updateBatchById(skillScontests);
        List<GroupResults> groupResults = groupResultsMapper.sort();
        Collections.sort(groupResults);
        int sort = 1;
        int sort1 = 1;
        for (GroupResults groupResult : groupResults) {
            if (groupResult.getFlag()) {
                groupResult.setRanking(sort1);
                sort++;
            } else {
                groupResult.setRanking(sort++);
                sort1 = sort;
            }
        }
        QueryWrapper<GroupResults> groupResultsQueryWrapper = new QueryWrapper<>();
        this.remove(groupResultsQueryWrapper);
        this.saveBatch(groupResults);
    }

    /**
     * 团体排序列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<GroupResults> groupRankingList(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        QueryWrapper<GroupResults> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("ranking");
        List<GroupResults> rankings = baseMapper.selectList(wrapper);
        PageInfo<GroupResults> info = new PageInfo<>(rankings);
        return info;
    }
}
